public class MySingleList implements ILest {

    //内部静态类
    static class ListNode {
        public int val;//数值域
        public ListNode next;//下一节点

        public ListNode(int val) {//构造方法
            this.val = val;
        }
    }
    //链表的属性  链表的头节点
    public ListNode head;//null

    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        this.head = node1;
    }
    //打印链表
    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public ListNode display(ListNode node) {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
        return head;
    }
    //头插
    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        node.next = head;
        this.head = node;

    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        ListNode cur = head;
        if(head == null) {//为空情况
            head = node;
        } else {
          while(cur.next != null) {//找到最后的尾结点数值域
              cur = cur.next;
          }
          cur.next = node;
        }
    }
    //指定位置插入
    @Override
    public void addIndex(int index, int data) throws IndexException {
        //判断index合法性
        if(index < 0 || index > size()) {
           throw new IndexException("index不合法" + index);
        }
        ListNode node = new ListNode(data);
        //判断List是否为空
        if(head == null) {
            head = node;
            return;
        }
        //头插
        if(index == 0) {
            addFirst(data);
            return;
        }
        //尾插
        if(index == size()) {
            addLast(data);
            return;
        }
        //中间部分
        ListNode cur = searchPrevIndex(index);
        node.next = cur.next;
        cur.next = node;

    }
    //寻找cur位置
    private ListNode searchPrevIndex(int index) {
        ListNode cur = head;
        int count = 0;
        while(count != index - 1) {//找到index前一个位置
            cur = cur.next;
            count++;
        }
        return cur;
    }
    //判断key是否存在
    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //如果ListNode为空
        if(head == null) {
            return;
        }
        //删除head
        if(head.val == key) {
            head = head.next;
            return;
        }
        //删除指定位置
        //找到删除位置前驱
        ListNode cur = findPrevKey(key);
        if(cur == null) {
            return;
        }
        cur.next = cur.next.next;
    }
    private ListNode findPrevKey(int key) {
        ListNode cur = head;
        while(cur.next != null ) {//遍历数组
            if(cur.next.val == key) {
                return cur;
            }else {
                cur = cur.next;
            }
        }
        return null;
    }
    @Override
    public void removeAllKey(int key) {//删除链表中的所有key
        if(head == null) {//为空
            return;
        }
        ListNode prev = head;
        ListNode cur = head.next;
        //头节点特殊情况 放在删除其他位置完成之后 或者使用循环删除头节点 因为可能头节点后面还可能是key
        while(cur != null) {
            if(cur.val == key) {//相等
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //删除完成后判断头节点
        if(head.val == key) {
            head = head.next;
        }
    }
    //链表个数
    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        head = null;
    }
    //翻转链表
    public ListNode reverseList() {
        //头插法翻转
        //为空
        if(head == null) {
            return null;
        }
        //只有一个
        if(head.next == null) {
            return head;
        }
        //多个链表元素
        ListNode cur = head.next;//找到后面head后面节点
        head.next = null;//头节点为空 变为尾结点
        while(cur != null) {
            //新建curNext 防止找不到cur.next链表元素位置
            ListNode curNext = cur.next;
            cur.next = head;
            //更新元素位置
            head = cur;
            cur = curNext;
        }
        return head;
    }
    //找中间节点
    public ListNode middleNode() {
        //快慢指针 1 2
        ListNode slow = head;
        ListNode fast = head;
        while(fast != null && fast.next != null) {//奇数偶数问题 偶数 奇数 先判断fast 再判断fast.next
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}
